package aim.orderentry.business;

import java.util.Date;

import aim.orderentry.MainActivity;
import aim.orderentry.R;
import aim.orderentry.database.Adapter.MySqliteManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;


public class GetLocationReceiver extends BroadcastReceiver {

	private AlarmManager 	alarmManager;
	private Context 		mContext;
	private LocationManager locationManager;
//	private MySqliteManager mySqliteManager;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		mContext = context;
		locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, mLocationListener);
	}
	
	private LocationListener mLocationListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		
		@Override
		public void onProviderEnabled(String provider) {
		}
		
		@Override
		public void onProviderDisabled(String provider) {
		}
		
		@Override
		public void onLocationChanged(Location location) {
			MapView map = MainActivity.getMapView();
			
			MyItemizedOverlay myItemizedOverlay = new MyItemizedOverlay(mContext.getResources().getDrawable(R.drawable.mark_blue),mContext.getApplicationContext());
//			MyItemizedOverlay myItemizedOverlay = new MyItemizedOverlay(mContext.getResources().getDrawable(R.drawable.mark_blue));
			GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
			myItemizedOverlay.addOverlay(new OverlayItem(point, "", ""));
			map.getOverlays().clear();
			map.getOverlays().add(myItemizedOverlay);
			map.getController().animateTo(point);
			locationManager.removeUpdates(mLocationListener);
			
			
			MySqliteManager mySqliteManager = new MySqliteManager(mContext);
			mySqliteManager.open();
			boolean resulAdded = mySqliteManager.inserData(location, new Date().toString());
			Toast.makeText(mContext, "" + resulAdded, Toast.LENGTH_LONG).show();
		}
	};
	
	public void setAlarm(int seconds,Context context) {
		alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(context, GetLocationReceiver.class);
		PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0);
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), seconds * 1000, pending);
	}
	
	public void cancelAlarm(Context context) {
		Intent intent = new Intent(context, GetLocationReceiver.class);
		PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0);
		alarmManager.cancel(pending);
	}
}
